using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Ideopuzzle.Resources;

namespace Ideopuzzle
{
    public class Credits : Menu
    {
        #region Fields
        
        SpriteFont font24;
        AudioManager audio_manager;

        #endregion

        public Credits(Game game)
            : base(game)
        {
            
        }

        public override void Initialize()
        {
            Game.Components.Add(new InputManager(Game));
            input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_manager.OnTap += new InputManager.Tap(OnTap);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            audio_manager = Game.Services.GetService(typeof(AudioManager)) as AudioManager;
            font24 = Game.Content.Load<SpriteFont>("Fonts/Light24");

            CreateButtonsList();
            LoadRenderableObjects();

            base.LoadContent();
        }

        protected override void Dispose(bool disposing)
        {
            if(input_manager!=null)
                input_manager.OnPress -= new InputManager.Tap(OnTap);
            Game.Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            {
                ScreenManager.SelectMainMenu(Game);
                audio_manager.PlayClick();
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);
        }

        #region Private Methods

        private void CreateButtonsList()
        {
            entries = new List<MenuEntry>();

            var e = new TextureButton(sprite_batch, Game.Content.Load<Texture2D>("Textures/Shared/menuback"),
                new Vector2(GraphicsDevice.Viewport.Width / 2.0f - Game.Content.Load<Texture2D>("Textures/Shared/menuback").Width / 2.0f, 400));

            e.OnPress +=new Press(GetButtonEventHandler(0));

            entries.Add(e);
        }

        private void LoadRenderableObjects()
        {
            renderables = new List<RenderableObject>();

            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/MainMenu/menubackground"), 
                Vector2.Zero));
            
            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/MainMenu/menulogo"),
                new Vector2(GraphicsDevice.Viewport.Width / 2.0f - Game.Content.Load<Texture2D>("Textures/MainMenu/menulogo").Width / 2.0f, 78)));
            renderables.Add(new TextObject(sprite_batch, font24, Lang.C_Developers, 136, Color.White));
            renderables.Add(new TextObject(sprite_batch, font24, "Giuseppe Maggiore", 170, new Color(37, 160, 218)));
            renderables.Add(new TextObject(sprite_batch, font24, "Andrea Gasparetto", 205, new Color(37, 160, 218)));
            renderables.Add(new TextObject(sprite_batch, font24, "Stefano Peranzoni", 240, new Color(37, 160, 218)));
            
            renderables.Add(new TextObject(sprite_batch, font24, Lang.C_Graphics, 300, Color.White));
            renderables.Add(new TextObject(sprite_batch, font24, "Luca Trinchero", 334, new Color(241, 151, 32)));
        }

        private Action<MenuEntry> GetButtonEventHandler(int index)
        {
            switch (index)
            {
                case 0:
                    return (MenuEntry m) => { ScreenManager.SelectMainMenu(Game); audio_manager.PlayClick(); };
                default:
                    return (MenuEntry m) => { Game.Exit(); };
            }
        }

        #endregion
    }
}
